package com.zrx.controller;

import com.zrx.domain.ResponseResult;
import com.zrx.domain.dto.ChangeStatusDto;
import com.zrx.domain.entity.Link;
import com.zrx.domain.vo.PageVo;
import com.zrx.service.LinkService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 张荣欣
 * @create ：2022/10/10  15:00
 */
@RestController
@RequestMapping("/content/link")
public class LinkController {

    @Autowired
    private LinkService linkService;

    /**
     * 查询友链 分页
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有分类 分页",notes = "查询所有分类 分页")
    public ResponseResult<PageVo> list(Integer pageNum, Integer pageSize, Link Link){
        return linkService.pageLinkList(pageNum,pageSize,Link);
    }

    /**
     * 删除友链
     * @param ids
     * @return
     */
    @DeleteMapping("/{id}")
    public ResponseResult deleteLink(@PathVariable("id") List<Long> ids){
        linkService.removeByIds(ids);
        return ResponseResult.okResult();
    }

    /**
     * 根据id获取友链
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult getByIdOneLink(@PathVariable("id") Long id){

        return ResponseResult.okResult(linkService.getById(id));
    }

    /**
     * 新增友链
     * @param link
     * @return
     */
    @PostMapping("")
    public ResponseResult addLink(@RequestBody Link link){
        linkService.save(link);
        return ResponseResult.okResult();
    }

    /**
     * 更新友链
     * @param link
     * @return
     */
    @PutMapping("")
    public ResponseResult updateLink(@RequestBody Link link){
        linkService.updateById(link);
        return ResponseResult.okResult();
    }
    /**
     * 更改状态
     * @param statusDto
     * @return
     */
    @PutMapping("/changeLinkStatus")
    public ResponseResult changeStatus(@RequestBody ChangeStatusDto statusDto){
        Link link = new Link();
        link.setId(statusDto.getId());
        link.setStatus(statusDto.getStatus());
        return ResponseResult.okResult(linkService.updateById(link));
    }
}
